package udes.assassin;

import udes.assassin.R;
import udes.assassin.server.api.Message.Message;
import udes.assassin.server.api.Message.GameSelectionMessage.RegisterMessage;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener, MessageManager{

	private AssassinApplication mAssassin;
	private ApplyMessageTask mTask;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		mAssassin = (AssassinApplication) getApplication();
      	mAssassin.mCurrentManager = this;
		
      	// Layout
      	setContentView(R.layout.activity_main);
      	
      	// Button listner
      	((Button) findViewById(R.id.button_main_to_game_selection)).setOnClickListener(this);
      	((Button) findViewById(R.id.button_main_to_prefs)).setOnClickListener(this);
      	((Button) findViewById(R.id.button_main_to_rules)).setOnClickListener(this);
      	((Button) findViewById(R.id.button_reconnect)).setOnClickListener(this);
    }
	
	@Override
	public void onPause(){
		super.onPause();
		mTask.stop();
	}
	
	@Override
	public void onResume(){
		super.onResume();
		
		// On page load we update name info
		SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
		String username = sharedPrefs.getString("pref_username", getString(R.string.default_username));
		((TextView) findViewById(R.id.value_username)).setText(username);
		
		// Play button is enable if we are connected to the server
      	((Button) findViewById(R.id.button_main_to_game_selection)).setEnabled(Global.getInstance().mIsConnectToServer);
      	
		if(Global.getInstance().mIsConnectToServer){
			Log.d("MainActivity", "Send register request");
			mAssassin.sendMessage(new RegisterMessage(Global.getInstance().mId, username));
		}
		else{
			Log.d("MainActivity", "You are not connected to server !");
		}

		// this should not happen
    	if(mTask != null){
    		mTask.stop();
    	}
    	
    	// Start 
		mTask = new ApplyMessageTask(this, mAssassin);
        mTask.execute(1);
	}
		
	public void onClick(View view) {
        Button clickedBtn = (Button) view;

        switch (clickedBtn.getId()) {
        	case R.id.button_main_to_game_selection:
            	startActivity(new Intent(this, GameSelectionActivity.class));
                break;
        	case R.id.button_main_to_prefs: 
        		startActivity(new Intent(this, PrefsActivity.class));
        		break;
        	case R.id.button_main_to_rules: 
        		startActivity(new Intent(this, RulesActivity.class));
        		break;
        	case R.id.button_reconnect: 
        		mAssassin.restartServerConnection();
        		break;
        }
    }

	public boolean manage(Message aMessage) {
		if(aMessage.getClass().equals(RegisterMessage.class)) {
			RegisterMessage casted = (RegisterMessage)aMessage;
			
			Log.d("Registering", "Should set button enabled");
			if(casted.isError){
				// TODO promp invalid Id message ???
				//mAssassin.sendMessage(new RegisterMessage(Global.getInstance().mId));
				Log.d("MainActivity", "Server hate us!!!" + casted.playerName);
			}
			else{
				//Global.getInstance().mIsConnectToServer = true;
				((Button) findViewById(R.id.button_main_to_game_selection)).setEnabled(true);
			}
			return true;
		}
		return false;
	}
}
